<%-- 
/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
--%>

<%@page import="edu.fpt.dal.ApartmentDAL"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="edu.fpt.dal.UsersDAL"%>
<%@page import="edu.fpt.hib.Users"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Searching for user</title>
        <style type="text/css"> <!--@import url("../style.css");--></style>
        <script>function goBack()  {  window.history.back()  }</script>
    </head>
    <body>
        <jsp:include page="template/header.jsp"/>
        <div id="page">
            <form method="GET" action="Search.jsp">

            </form>
            <%
                String keyword = request.getParameter("keyword");
                ArrayList<Users> list = new ArrayList<Users>();
                try {
                    list = UsersDAL.searchUsers(keyword);

                } catch (Exception e) {
                    out.println("Error: " + e.getMessage());
                }%>
            <div id="inset"><h1>Search result!</h1></div>
            <%
                if (list.isEmpty()) {
            %>
            <div id="inset"><h3>No record found! Try another keyword!</h3><input class="searchsubmit" type='button' value='Back' onclick='goBack()'></div>
                <%} else {%>

            <div id="inset"><h3><%= list.size()%> records found</h3><input class="searchsubmit" type='button' value='Back' onclick='goBack()'></div>

            <table id="newspaper-a" border='0'>
                <tr><th scope="col">User ID</th>
                    <th scope="col">Full Name</th>
                    <th scope="col">User Role</th>
                    <th scope="col">Created Date</th>
                    <th scope="col">Apartment</th>
                </tr>
                <%
                    Iterator<Users> it = list.iterator();
                    while (it.hasNext()) {
                        Users entity = it.next();%>
                <h4><tr><td><a href="detailuser.jsp?userid=<%=entity.getUserId()%>"><%= entity.getUserId()%></td>
                        <td><%= entity.getUserFullName()%></td>
                            <td><% int role = entity.getUserRole();
                                        if (role == 1) {%> Administrator
                            <%} else {%>
                            Normal User
                            <%}
                            %></td>
                        <td><%= entity.getUserCreateDate()%></td>
                        <td><%= ApartmentDAL.getApartmentByUser(entity.getUserId())%></td>
                        <td><form method="get" action="edituser.jsp">
                                <input type="hidden" name="userid" value="<%=entity.getUserId()%>">
                                <input class="searchsubmit" type="submit" value="Edit">
                            </form></td>
                        <td><form method="get" action="deleteuser.jsp">
                                <input type="hidden" name="userid" value="<%=entity.getUserId()%>">
                                <input class="searchsubmit" type="submit" value="Delete">
                            </form></td>
                    </tr></h4>
                    <%   }%>

            </table>
            <% }

            %>



        </div>
        <jsp:include page="template/footer.jsp"/>
    </body>
</html>
